﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Canyala.Lagoon.Extensions;

namespace Canyala.Lagoon.Controls
{
    public class TextBox : System.Windows.Controls.TextBox
    {


        public ICommand CommandOnEnter
        {
            get { return (ICommand)GetValue(CommandOnEnterProperty); }
            set { SetValue(CommandOnEnterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandOnEnter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandOnEnterProperty =
            DependencyProperty.Register("CommandOnEnter", typeof(ICommand), typeof(Controls.TextBox), null);


        protected override void OnKeyUp(KeyEventArgs e)
        {
            GetBindingExpression(TextBox.TextProperty).UpdateSource();
            base.OnKeyUp(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (Text.HasValue())
                {
                    if (CommandOnEnter != null)
                        CommandOnEnter.Execute(null);
                }
            }

            base.OnKeyDown(e);
        }

    }
}
